<?php

namespace app\api\controller;

use app\api\model\AlbumMusicModel;
use app\api\model\AlbumSkinModel;
use app\api\model\outer\MusicCateModel;
use app\api\model\outer\MusicModel;
use app\api\model\outer\SkinCateModel;
use app\api\model\outer\SkinModel;
use app\common\model\project_model\AlbumMusicCategoryModel;
use app\common\model\project_model\AlbumSkinCategoryModel;
use app\common\model\project_model\ArticleRecordModel;
use think\Controller;

/**
 * 执行文件
 *
 * Class Console
 * @package app\api\controller
 */
class Console extends Controller {
    /**
     * 从原平台获取音乐到现有平台数据库
     */
    public function musicPull() {
        $type = input('type/d', 0);// 0为添加，1为删除再新增
        // 原有平台数据
        $data = MusicModel::getAll();
        $data = $this->formatMusic($data);
        $musicM = new AlbumMusicModel();
        if ($type) {
            $musicM->where('id', '>', 0)->delete();
        }
        $res = $musicM->saveAll($data);
        if (!$res) {
            return json(['code' => 0, 'error' => 'error']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 格式化音乐数据
     * @param $data
     * @return array
     */
    private function formatMusic($data) {
        $list = [];
        foreach ($data as$key => $v) {
            $list[$key] = $v;
            $list[$key]['created_time'] = date('Y-m-d H:i:s', time());
            $list[$key]['updated_time'] = date('Y-m-d H:i:s', time());
        }
        return $list;
    }

    /**
     * 从原平台迁移音乐分类数据
     */
    public function musicCatePull() {
        $type = input('type/d', 1);// 0为添加，1为删除再新增
        $data = MusicCateModel::getCate();
        $list = [];
        foreach ($data as $k => $v) {
            $list[$k] = $v;
            $list[$k]['created_time'] = date('Y-m-d H:i:s', time());
            $list[$k]['updated_time'] = date('Y-m-d H:i:s', time());
        }

        //var_dump($list);die;
        $model = new AlbumMusicCategoryModel();
        if ($type) {
            $model->where('id', '>', 0)->delete();
        }
        $res = $model->saveAll($list, false);
        if (!$res) {
            return json(['code' => 0, 'error' => 'error']);
        }
        return json(['code' => 1, 'msg' => 'success']);

    }

    /**
     * 从原平台迁移模板分类数据
     */
    public function skinCatePull() {
        $type = input('type/d', 1);// 0为添加，1为删除再新增
        $data = SkinCateModel::getCate();
        $list = [];
        foreach ($data as $k => $v) {
            $list[$k] = $v;
            $list[$k]['created_time'] = date('Y-m-d H:i:s', time());
            $list[$k]['updated_time'] = date('Y-m-d H:i:s', time());
        }

        $model = new AlbumSkinCategoryModel();
        if ($type) {
            $model->where('id', '>', 0)->delete();
        }
        $res = $model->saveAll($list, false);
        if (!$res) {
            return json(['code' => 0, 'error' => 'error']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 从原平台获取模板到现有平台数据库
     */
    public function skinPull() {
        $type = input('type/d', 0);// 0为添加，1为删除再新增
        // 原有平台数据
        $data = SkinModel::getAll();
        $data = $this->formatSkin($data);
        $skinM = new AlbumSkinModel();
        if ($type) {
            $skinM->where('id', '>', 0)->delete();
        }
        $res = $skinM->saveAll($data);
        if (!$res) {
            return json(['code' => 0, 'error' => 'error']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 格式化音乐数据
     * @param $data
     * @return array
     */
    private function formatSkin($data) {
        $list = [];
        foreach ($data as$key => $v) {
            $list[$key] = $v;
            $list[$key]['created_time'] = date('Y-m-d H:i:s', time());
            $list[$key]['updated_time'] = date('Y-m-d H:i:s', time());
        }
        return $list;
    }

    public function test() {
        $r = [];
        if ($r) {
            echo '空也';
        } else {
            echo "居然不为空";
        }die;
        $articleRecordM = new ArticleRecordModel();
        $ARList = $articleRecordM->where(['wechat_id' => 1])->distinct(true)->column('article_id');
    }
}